package com.sqlcheck.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Map;

@Data
@Schema(description = "扫描任务响应")
public class ScanTaskResponse {

    @Schema(description = "任务ID", example = "1")
    private Long id;

    @Schema(description = "任务名称", example = "项目A的SQL兼容性扫描")
    private String taskName;

    @Schema(description = "任务类型", example = "PROJECT_SCAN", allowableValues = { "PROJECT_SCAN", "GIT_SOURCE_SCAN", 
            "DATABASE_DDL_SCAN", "MANUAL_SQL" })
    private String taskType;

    @Schema(description = "任务状态", example = "RUNNING", allowableValues = { "PENDING", "RUNNING", "SUCCESS", "FAILED",
            "CANCELLED" })
    private String status;

    @Schema(description = "任务进度(0-100)", example = "75", minimum = "0", maximum = "100")
    private Integer progress;

    @Schema(description = "关联的项目ID", example = "1")
    private Long projectId;

    @Schema(description = "项目名称", example = "项目A")
    private String projectName;

    @Schema(description = "分支名称", example = "main")
    private String branchName;

    @Schema(description = "创建用户ID", example = "1")
    private Long userId;

    @Schema(description = "创建用户名", example = "张三")
    private String username;

    @Schema(description = "任务描述", example = "对项目A进行Oracle到GoldenDB的兼容性扫描分析")
    private String description;

    @Schema(description = "任务配置参数", example = """
            {
                "sourceDbType": "ORACLE",
                "targetDbType": "GOLDENDB",
                "scanRules": ["SYNTAX_CHECK", "COMPATIBILITY_CHECK"],
                "includePatterns": ["**/*.sql", "**/*.java"],
                "excludePatterns": ["**/test/**"]
            }
            """)
    private Map<String, Object> config;

    @Schema(description = "DDL提取统计信息", example = """
            {
                "totalStatements": 23,
                "extractionTimeMs": 1907,
                "databaseInfo": {
                    "host": "115.159.214.105",
                    "port": 3306,
                    "database": "codeknowledge",
                    "dbType": "MYSQL",
                    "dbVersion": "5.5.5"
                },
                "objectCounts": {
                    "表": 9,
                    "索引": 14
                },
                "totalContentSize": 14497
            }
            """)
    private Map<String, Object> extractionStats;

    @Schema(description = "任务开始时间", example = "2023-12-01T10:30:00")
    private LocalDateTime startTime;

    @Schema(description = "任务结束时间", example = "2023-12-01T11:30:00")
    private LocalDateTime endTime;

    @Schema(description = "错误信息，任务失败时包含具体错误详情", example = "连接数据库超时")
    private String errorMessage;

    @Schema(description = "报告文件路径", example = "/reports/scan_20231201_103000.pdf")
    private String reportPath;

    @Schema(description = "创建时间", example = "2023-12-01T10:00:00")
    private LocalDateTime createdAt;

    @Schema(description = "更新时间", example = "2023-12-01T11:30:00")
    private LocalDateTime updatedAt;

    @Schema(description = "当前重试次数", example = "1", minimum = "0")
    private Integer retryCount;

    @Schema(description = "最大重试次数", example = "3", minimum = "0")
    private Integer maxRetryCount;

    @Schema(description = "最后重试时间", example = "2023-12-01T12:00:00")
    private LocalDateTime lastRetryTime;

    @Schema(description = "是否可以重试", example = "true")
    private Boolean canRetry;

    // 增强字段：用于前端统一显示，避免解析config的复杂性
    @Schema(description = "分析引擎类型（从config中提取）", example = "AI引擎", allowableValues = { "规则引擎", "AI引擎", "混合模式" })
    private String analysisEngineDisplay;
    
    @Schema(description = "数据源信息（从config中提取）", example = "Oracle: 168.63.18.127:1521/servdb")
    private String dataSourceDisplay;
    
    @Schema(description = "源数据库类型（从config中提取）", example = "ORACLE")
    private String sourceDbType;
    
    @Schema(description = "目标数据库类型（从config中提取）", example = "GOLDENDB") 
    private String targetDbType;

    @Schema(description = "是否启用AI增强分析", example = "false")
    private Boolean aiEnhancementEnabled;

    @Schema(description = "AI分析状态", example = "COMPLETED", allowableValues = { "PENDING", "RUNNING", "COMPLETED", "SKIPPED", "FAILED" })
    private String aiAnalysisStatus;

    @Schema(description = "AI分析开始时间", example = "2023-12-01T10:35:00")
    private LocalDateTime aiAnalysisStartTime;

    @Schema(description = "AI分析结束时间", example = "2023-12-01T10:40:00")
    private LocalDateTime aiAnalysisEndTime;

    @Schema(description = "AI分析处理时长（毫秒）", example = "5000")
    private Long aiAnalysisDuration;

    @Schema(description = "AI分析处理的SQL数量", example = "15")
    private Integer aiProcessedSqlCount;

    @Schema(description = "AI分析跳过原因", example = "规则分析未发现问题")
    private String aiSkipReason;

    @Schema(description = "AI筛选配置", example = """
            {
                "severityThreshold": "MEDIUM",
                "maxStatements": 50,
                "timeoutSeconds": 60,
                "enableParallelProcessing": true
            }
            """)
    private Map<String, Object> aiFilterConfig;
}